import { mergeProps as _mergeProps, createVNode as _createVNode } from "vue";
import { nextTick, defineComponent } from "vue";
import { pick, extend, truthProp, makeArrayProp, makeStringProp, createNamespace, HAPTICS_FEEDBACK } from "../utils/index.mjs";
import { Icon } from "../icon/index.mjs";
import { Popup } from "../popup/index.mjs";
import { Loading } from "../loading/index.mjs";
import { popupSharedProps, popupSharedPropKeys } from "../popup/shared.mjs";
const [name, bem] = createNamespace("action-sheet");
// 多用于底部弹出选择数据 可以在内部插入picker组件

// 数据结构
// [
//   { name: '选项一' },
//   { name: '选项二' },
//   { name: '选项三', subname: '描述信息' },
// ];
const actionSheetProps = extend({}, popupSharedProps, {
  title: String, // 顶部标题
  round: truthProp,
  actions: makeArrayProp(), // 选择数据
  closeIcon: makeStringProp("cross"),
  closeable: truthProp,
  cancelText: String,
  description: String,
  closeOnPopstate: truthProp, // 是否在页面回退时自动关闭
  closeOnClickAction: Boolean, // 是否在点击选项后关闭
  safeAreaInsetBottom: truthProp // 是否开启底部安全区适配
});

// 从本次props中picker部分设定给prop组件
const popupInheritKeys = [...popupSharedPropKeys, "round", "closeOnPopstate", "safeAreaInsetBottom"];
var stdin_default = defineComponent({
  name,
  props: actionSheetProps,
  emits: ["select", "cancel", "update:show"],
  setup(props, {
    slots,
    emit
  }) {
    // 接收popup组件双向绑定返回值, 更新本组件双向绑定事件
    const updateShow = (show) => emit("update:show", show);
    const onCancel = () => {
      updateShow(false);
      emit("cancel");
    };
    // 顶部title信息渲染函数, 可以选择插入closeIcon
    const renderHeader = () => {
      if (props.title) {
        return _createVNode("div", {
          "class": bem("header")
        }, [props.title, props.closeable && _createVNode(Icon, {
          "name": props.closeIcon,
          "class": [bem("close"), HAPTICS_FEEDBACK],
          "onClick": onCancel
        }, null)]);
      }
    };
    // 顶部title下面描述渲染函数,slots.description可以自定义渲染函数
    const renderDescription = () => {
      if (props.description || slots.description) {
        const content = slots.description ? slots.description() : props.description;
        return _createVNode("div", {
          "class": bem("description")
        }, [content]);
      }
    };
    // 底部取消按钮渲染函数 可以通过slots.cancel插槽自定义 或 默认props.cancelText
    // van-action-sheet__gap为添加的间隔线
    const renderCancel = () => {
      if (slots.cancel || props.cancelText) {
        return [_createVNode("div", {
          "class": bem("gap")
        }, null), _createVNode("button", {
          "type": "button",
          "class": bem("cancel"),
          "onClick": onCancel
        }, [slots.cancel ? slots.cancel() : props.cancelText])];
      }
    };
    // 按钮内部右侧渲染函数
    const renderIcon = (action) => {
      if (action.icon) {
        return _createVNode(Icon, {
          "class": bem("item-icon"),
          "name": action.icon
        }, null);
      }
    };
    // 按钮内部渲染函数
    const renderActionContent = (action, index) => {
      if (action.loading) {
        return _createVNode(Loading, {
          "class": bem("loading-icon")
        }, null);
      }
      // 根据slotsaction自定义 展示选项内容 传递插槽数据 action及index
      if (slots.action) {
        return slots.action({
          action,
          index
        });
      }
      return [
        // 按钮名称
        _createVNode("span", {
          "class": bem("name")
        }, [action.name]), 
        // 按钮信息描述
        action.subname && _createVNode("div", {
          "class": bem("subname")
        }, [action.subname])
      ];
    };

    // 选择按钮渲染函数
    // 通过action 处理action数据上item各项特定设置
    // 统一处理 disable loading callback closeOnClickAction
    // 外部包装一层button, 语义化更好, 统一处理style及className
    const renderAction = (action, index) => {
      const {
        color,
        loading, // 是否loading
        callback,
        disabled,
        className
      } = action;
      const onClick = () => {
        if (disabled || loading) {
          return;
        }
        if (callback) {
          callback(action);
        }
        if (props.closeOnClickAction) {
          updateShow(false);
        }
        nextTick(() => emit("select", action, index));
      };
      return _createVNode("button", {
        "type": "button",
        "style": {
          color
        },
        "class": [bem("item", {
          loading,
          disabled
        }), className],
        "onClick": onClick
      }, [renderIcon(action), renderActionContent(action, index)]);
    };

    
    return () => _createVNode(Popup, _mergeProps({
      "class": bem(),
      "position": "bottom",
      "onUpdate:show": updateShow
    }, pick(props, popupInheritKeys)), {
      default: () => {
        var _a;
        return [renderHeader(), renderDescription(), _createVNode("div", {
          "class": bem("content")
          // 通过actions数据 生成按钮
        }, [props.actions.map(renderAction), (_a = slots.default) == null ? void 0 : _a.call(slots)]), renderCancel()];
      }
    });
  }
});
export {
  actionSheetProps,
  stdin_default as default
};
